
/*
 * Java class "Scanner.java" generated from Poseidon for UML.
 * Poseidon for UML is developed by <A HREF="http://www.gentleware.com">Gentleware</A>.
 * Generated with <A HREF="http://jakarta.apache.org/velocity/">velocity</A> template engine.
 */
package jcs;

import robocode.*;


/**
 * 
 * @poseidon-object-id [sm$89848d:ffe131bfd5:-7f8b]
 */
public class Scanner {

   ///////////////////////////////////////
   // associations

/**
 * 
 * @poseidon-object-id [sm$89848d:ffe131bfd5:-7fe1]
 */
    private Seth seth; 


  ///////////////////////////////////////
  // operations


/**
 * 
 * 
 * @poseidon-object-id [sm$89848d:ffe131bfd5:-7fe0]
 * @return 
 * @param _seth 
 */
    public  Scanner(Seth _seth) {        
        		seth = _seth;
    } // end Scanner        

/**
 * ******* Do Scan Events **************
 * 
 * @poseidon-object-id [sm$89848d:ffe131bfd5:-7fdf]
 */
    public void doScan() {        
        		double heading;
                        EnemyList enemies = seth.getEnemyList();
                        if(enemies.getCount() == seth.getOthers() && enemies.getCount() != 0)
        		{
        			Enemy oldest = enemies.getOldest();
                                if(oldest.isSeeking() == false)
                                {
                                    heading = seth.getCurrentPos().calcHeading( oldest.getCoord() );
                                    if(heading > seth.getRadarHeadingRadians())
                                    {
                                      	if(heading - seth.getRadarHeadingRadians() > Math.PI)
        					seth.setTurnRadarLeftRadians( 2.0 * Math.PI);
                                        else
        					seth.setTurnRadarRightRadians( 2.0 * Math.PI);
                                    }
                                    else
                                    {
        				if(heading - seth.getRadarHeadingRadians() > Math.PI)
        					seth.setTurnRadarRightRadians( 2.0 * Math.PI);
        				else
        					seth.setTurnRadarLeftRadians( 2.0 * Math.PI);
                                    }
                                    oldest.setSeeking(true);
                                }
        		}
        		else
        		{
        			seth.setTurnRadarLeftRadians( 2.0 * Math.PI);
        		}
    } // end doScan        

/**
 * ******* On Scanned Events **************
 * 
 * @poseidon-object-id [sm$89848d:ffe131bfd5:-7fdd]
 * @param e 
 */
    public void scannedRobotEvent(ScannedRobotEvent e) {        
                        EnemyList enemies = seth.getEnemyList();
                        // Attept to load existing enemy, otherwise creates a new one 
                        Enemy enemy = enemies.get(e.getName());
                        
                        // Calculates a bearing from the top of the screen rather than from our current heading
        		// to the target Coordinate
        		double bearingFromZero = seth.getHeadingRadians() + e.getBearingRadians();
        		if(bearingFromZero > Math.PI *  2.0)
        			bearingFromZero -= Math.PI *  2.0;
                        
                        enemy.setCoord(seth.getCurrentPos().getCoordFromScan(bearingFromZero, e.getDistance()));
                        enemy.setVelocity(e.getVelocity());
                        enemy.setBearing(e.getHeadingRadians());
                        enemy.setRelativeBearing(e.getBearingRadians() + seth.getHeadingRadians());
                        enemy.setEnergy(e.getEnergy());
                        enemy.setTicks(seth.getTicks());
                        enemy.setSeeking(false);
                        
                        //System.out.println("At tick " +seth.getTicks()+ " I see "+enemy.getName() + " at " + enemy.getCoord().toString());
    } // end scannedRobotEvent        

 } // end Scanner


